package shop.controller.front.home;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.account.AccountService;
import shop.services.front.account.bean.Account;
import shop.services.front.area.AreaService;
import shop.services.front.area.bean.Area;
import shop.services.front.indexImg.IndexImgService;
import shop.services.front.indexImg.bean.IndexImg;
import shop.services.front.news.NewsService;
import shop.services.front.news.bean.News;
import shop.services.front.organize.OrganizeService;
import shop.services.front.organize.bean.Organize;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/18.
 */
@Controller
@RequestMapping("/")
public class HomeController extends FrontBaseController<News> {
    @Resource
    private NewsService newsService;
    @Resource
    private AreaService areaService;
    @Resource
    private IndexImgService indexImgService;
    @Resource
    private AccountService accountService;
    @Resource
    private OrganizeService organizeService;
    @Override
    public Services<News> getService() {
        return newsService;
    }


    /**
     * 页面跳转方法
     *
     * @return
     */
    @RequestMapping({"/", "/index"})
    public String toIndex() {
        return "/front/login";
    }
    @RequestMapping("toHome")
    public String toHome(){return "/front/home/home";}
    @RequestMapping("/toNewsDetail")
    public String toNewsList() {
        return "/front/home/newsDetail";
    }
    @RequestMapping("/toAreaList")
    public String toAreaList() {
        return "/front/home/areaList";
    }


    /**
     * 查询全部首页轮播图
     *
     * @param indexImg
     * @return
     */
    @RequestMapping(value = "/selectIndexImg", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectIndexImg(IndexImg indexImg) {
        JSONResult jsonResult = new JSONResult();
        List<IndexImg> list = indexImgService.selectList(indexImg);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询地级市
     * @param area
     * @return
     */
    @RequestMapping(value = "/selectArea", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectArea(Area area) {
        JSONResult jsonResult = new JSONResult();
        List<Area> list = areaService.selectList(area);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }
    @RequestMapping(value = "/getSession",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getSession(HttpSession session){
        JSONResult rs=new JSONResult();
        Map result=new HashMap<>();
        if (session.getAttribute("accountInfo")!=null){
        result.put("account",session.getAttribute("accountInfo"));
        }else {
            result.put("account",null);
        }
        if(session.getAttribute("organizeInfo")!=null){
        result.put("organize",session.getAttribute("organizeInfo"));
        }else {
            result.put("organize",null);
        }
        rs.setData(result);
        rs.setSuccess(true);
        return  rs;
    }
    @RequestMapping(value = "/updateSession",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult updateSession(HttpSession session){
        JSONResult rs=new JSONResult();
        Map result=new HashMap<>();
        if (session.getAttribute("accountInfo")!=null){
            Account account=(Account)session.getAttribute("accountInfo");
            account=accountService.selectOne(account);
            session.setAttribute("accountInfo",account);
            result.put("account",session.getAttribute("accountInfo"));
        }else {
            result.put("account",null);
        }
        if(session.getAttribute("organizeInfo")!=null){
            Organize organize=(Organize)session.getAttribute("organizeInfo");
            organize=organizeService.selectOne(organize);
            session.setAttribute("organizeInfo",organize);
            result.put("organize",session.getAttribute("organizeInfo"));
        }else {
            result.put("organize",null);
        }
        rs.setData(result);
        rs.setSuccess(true);
        return  rs;
    }
}